package org.example.dao.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import org.example.consttants.DataSourceType;
import org.example.dao.OrderDao;
import org.example.entity.Order;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @version 1.0.0
 * Created by cyd on 2022-09-23 14:58:25
 */
@Service
public class OrderDaoImpl implements OrderDao {

    @Resource
    JdbcTemplate jdbcTemplate;


    @Override
    public List<Order> getOrderListByUserId(Integer userId) {
//        StringBuilder sqlBuilder = new StringBuilder();
//        sqlBuilder
//                .append("select order_id, user_id from order where user_id=? ");
//        return jdbcTemplate.query(sqlBuilder.toString(), new Object[]{userId},
//                new int[]{Types.INTEGER}, new BeanPropertyRowMapper<Order>(
//                        Order.class));
        return null;
    }

    @DS(value = DataSourceType.READ_ONLY)
//    @DS(value = DataSourceType.READ_WRITE)
    @Override
    public List<Order> getOrderList() {
        String sql = "select order_id, user_id from order_0 ";

        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<Order>(
                Order.class));
    }

    @Override
    public void createOrder(Order order) {
        StringBuffer sb = new StringBuffer();
        sb.append("insert into order_0(user_id, order_id)");
        sb.append("values(");
        sb.append(order.getUserId()).append(",");
        sb.append(order.getOrderId());
        sb.append(")");
        jdbcTemplate.update(sb.toString());
    }

}
